package com.iut.projectandroid;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.iut.controls.CameraPreview;

public class CameraActivity extends Activity {

	public static final int MEDIA_TYPE_IMAGE = 1;
	public static final int MEDIA_TYPE_VIDEO = 2;
	
	protected static final int TAKE_PHOTO = 100;

	private CameraPreview preview;
	
	private Camera camera = null;
	
	private FrameLayout frame;
	
	private LinearLayout loadScreen;
	
	LoadCameraAsyncTask loadCamTask;
	
	private Button buttonPhoto;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_camera);
		
		frame = (FrameLayout) findViewById(R.id.camera_preview);
		
		loadScreen = (LinearLayout) findViewById(R.id.camera_load_screen);
		
		loadCamTask = new LoadCameraAsyncTask();
		
		buttonPhoto = (Button) findViewById(R.id.camera_take_photo);
		
		buttonPhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				if(camera != null)
				{
					camera.takePicture(null, null, pictureCallback);
				}
			}
		});
	}
	
	PictureCallback pictureCallback = new PictureCallback() {
		
		@Override
		public void onPictureTaken(byte[] data, Camera camera) 
		{
			quitCamera(data);
		}
	};
	
	private void quitCamera(byte[] data) 
	{
//		Bitmap bitmap = BitmapFactory.
		Intent retIntent = new Intent();
		retIntent.putExtra("PICTURE_DATA", data);
		setResult(RESULT_OK, retIntent);
		finish();
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) 
	{
		Log.d("PARCEL_TAG", "ON_WINDOW_FOCUS_CHANGED");
		if(hasFocus)
		{
			loadCamTask = new LoadCameraAsyncTask();
			
			if(loadCamTask.getStatus() == Status.PENDING)
			{
				Log.d("PARCEL_TAG", "Camera LOADING");
				loadCamTask.execute();
			}
		}
		super.onWindowFocusChanged(hasFocus);
	}
	
	@Override
	public void onBackPressed() 
	{
		if(loadCamTask != null && loadCamTask.getStatus() == Status.RUNNING)
			return;
		
		super.onBackPressed();
	}
	
	@Override
	protected void onResume() 
	{
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		getActionBar().hide();
		
		super.onResume();
	}
	
	@Override
	protected void onPause() 
	{
		if(loadCamTask != null && loadCamTask.getStatus() == Status.RUNNING)
		{
			Log.d("PARCEL_TAG", "Camera TASK_LOADING RUNNING");
			loadCamTask.cancel(false);
		}
		
		if(camera != null)
		{
			try
			{
				Log.d("PARCEL_TAG", "Camera RELEASE ON PAUSE");
				camera.stopPreview();
				camera.release();
				camera = null;
			}
			catch(Exception e) { }
		}
		
		frame.removeView(preview);
		
		loadCamTask = null;
		
		super.onPause();
	}
	
	/**
	 * 
	 * @return
	 */
	private Camera getCameraInstance() 
	{
		Camera cam = null;
		
		try
		{
			cam = Camera.open();
		}
		catch(Exception e) {
			
		}
		
		return cam;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.camera, menu);
		return true;
	}

	private class LoadCameraAsyncTask extends AsyncTask<Void, Void, Void> 
	{

		@Override
		protected Void doInBackground(Void... params)
		{
			camera = getCameraInstance();
			
			return null;
		}
		
		@Override
		protected void onCancelled(Void result) 
		{
			if(camera != null)
			{
				Log.d("PARCEL_TAG", "Camera RELEASE ON CANCEL");
				camera.release();
			}
		}
		
		@Override
		protected void onPostExecute(Void result) 
		{			
			preview = new CameraPreview(getApplicationContext(), camera);
				
			frame.addView(preview);
				
			loadScreen.setVisibility(View.GONE);
				
			frame.setVisibility(View.VISIBLE);
			
			buttonPhoto.setVisibility(View.VISIBLE);
			
			super.onPostExecute(result);
		}
	}
}
